local yujun = fk.CreateSkill{
    name = "ym2__yujun",
    tags = { Skill.Family },
}
Fk:loadTranslationTable{
    ["ym2__yujun"] = "驭骏",
    [":ym2__yujun"] = "宗族技，每轮限一次，你可以令一名同族角色获得/失去〖马术〗，以视为使用或打出一张【杀】/【闪】。",
    ["#ym2__yujun"] = "驭骏：令一名同族角色获得/失去〖马术〗，以视为使用或打出一张【杀】/【闪】",
    ["#ym2__yujun-slash"] = "驭骏：令一名同族角色获得〖马术〗，以视为使用或打出一张【杀】",
    ["#ym2__yujun-jink"] = "驭骏：令一名同族角色失去〖马术〗，以视为使用或打出一张【闪】",
}

local U = require "packages/utility/utility"

yujun:addEffect("viewas", {
    prompt = "#ym2__yujun",
    max_round_use_time = 1,
    interaction = function(self, player)
        local all_names = player:getViewAsCardNames(yujun.name, {"slash", "jink"})
        local names = {}
        if table.contains(all_names, "slash") and table.find(Fk:currentRoom().alive_players, function(p)
            return U.FamilyMember(player, p) and not p:hasSkill("mashu")
        end) then
            table.insert(names, "slash")
        end
        if table.contains(all_names, "jink") and table.find(Fk:currentRoom().alive_players, function(p)
            return U.FamilyMember(player, p) and p:hasSkill("mashu")
        end) then
            table.insert(names, "jink")
        end
        if #names == 0 then return end
        return UI.CardNameBox {choices = names}
    end,
    view_as = function(self, player)
        if self.interaction.data == nil then return end
        local card = Fk:cloneCard(self.interaction.data)
        card.skill = Fk.skills["ym2z_machao__slash_skill"]
        card.skillName = yujun.name
        return card
    end,
    before_use = function(self, player, use)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p)
            return U.FamilyMember(player, p) and
            ((use.card.trueName == "slash" and not p:hasSkill("mashu")) or (use.card.trueName == "jink" and p:hasSkill("mashu")))
        end)
        if #targets == 0 then return yujun.name end
        local tos = targets
        if #tos > 1 then
            tos = room:askToChoosePlayers(player, {
                max_num = 1,
                min_num = 1,
                targets = targets,
                skill_name = yujun.name,
                prompt = "#ym2__yujun-"..use.card.trueName,
                cancelable = false,
            })
        end
        if #tos > 0 then
            local to = tos[1]
            room:handleAddLoseSkills(to, (to:hasSkill("mashu") and "-" or "").."mashu")
        else
            return yujun.name
        end
    end,
    enabled_at_play = function(self, player)
        return table.find(Fk:currentRoom().alive_players, function(p)
            return U.FamilyMember(player, p) and not p:hasSkill("mashu")
        end) and self:withinTimesLimit(player, Player.HistoryRound)
    end,
    enabled_at_response = function(self, player)
        local all_names = player:getViewAsCardNames(yujun.name, {"slash", "jink"})
        if table.contains(all_names, "slash") and table.find(Fk:currentRoom().alive_players, function(p)
            return U.FamilyMember(player, p) and not p:hasSkill("mashu")
        end) and self:withinTimesLimit(player, Player.HistoryRound) then
            return true
        end
        if table.contains(all_names, "jink") and table.find(Fk:currentRoom().alive_players, function(p)
            return U.FamilyMember(player, p) and p:hasSkill("mashu")
        end) and self:withinTimesLimit(player, Player.HistoryRound) then
            return true
        end
    end,
})

yujun:addEffect("targetmod", {
    bypass_distances =  function(self, player, skill, card, target)
        if card and card.trueName == "slash" and player:hasSkill(yujun.name) and card.skillName == yujun.name then
            return player:getAttackRange() > player:distanceTo(target) - 2
        end
    end,
})

return yujun